<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-input v-model="query.jgxx" clearable size="small" placeholder="机构名称、机构编码" style="width: 170px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
        <el-button
          v-if="crud.optShow.download"
          :loading="crud.downloadLoading"
          :disabled="!crud.data.length"
          class="filter-item"
          size="mini"
          type="warning"
          icon="el-icon-download"
          @click="crud.doExport"
        >导出</el-button>
      </div>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="index" width="50" label="序号" align="center" />
        <el-table-column align="center" prop="bisHljgYljgbh" label="机构编号" width="90" />
        <el-table-column align="center" prop="bisHljgQymc" label="机构名称" />
        <el-table-column align="center" prop="dfpCount" label="已复核数量" />
        <el-table-column align="center" prop="yfpCount" label="管理状态">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.yfpCount === '1' ? 'success' : 'warning'"
              disable-transitions
            >{{ scope.row.yfpCount === '1' ? '管理中' : '已过期' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-grid"
              @click="openOrgList(scope.row)"
            >
              查 看
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <completeOrgList ref="completeOrgList" />
    </div>
  </div>
</template>

<script>
import completeOrgList from './completeOrgList'
import crudZhsq from '@/api/business/zhsq'
import crudFile from '@/api/tools/localStorage'
import { getAdlXmxx } from '@/api/business/adlXmxx'
import { queryPgAdlResult } from '@/api/business/AdlZhsq'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'

const defaultForm = { bisZhsqId: null, bisZhsqHljgId: null, bisZhsqSnryId: null, bisZhsqSnryXm: null, bisZhsqSnryXb: null, bisZhsqSnrySfz: null, bisSnryYbssqx: null, bisZhsqDykkrq: null, bisZhsqDyjsrq: null, bisZhsqXsdyq: null, bisZhsqAdl: null, bisZhsqZt: null }
export default {
  name: 'Zhsq',
  components: { pagination, rrOperation, udOperation, completeOrgList },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['xb', 'yjsgx', 'ccs', 'zhzt', 'ccstcqh'],
  cruds() {
    return CRUD({ title: '居家照护申请', url: 'api/sbgs/org/completed', idField: 'bisHljgId', sort: '', crudMethod: { ...crudZhsq }})
  },
  data() {
    return {
      adlDrawer: false,
      snryDrawer: false,
      snryData: [],
      adlData: [],
      tempFiles: [],
      files: [],
      files2: [],
      roleId: 0,
      permission: {
        add: ['admin', 'zhsq:add'],
        edit: ['admin', 'zhsq:edit'],
        del: ['admin', 'zhsq:del']
      },
      rules: {
        bisZhsqSnryXm: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        bisZhsqSnryXb: [
          { required: true, message: '性别不能为空', trigger: 'blur' }
        ],
        bisZhsqSnrySfz: [
          { required: true, message: '身份证不能为空', trigger: 'blur' }
        ],
        bisSnryYbssqx: [
          { required: true, message: '医保所属区县不能为空', trigger: 'blur' }
        ],
        bisZhsqDykkrq: [
          { required: true, message: '待遇开始日期不能为空', trigger: 'blur' }
        ],
        bisZhsqDyjsrq: [
          { required: true, message: '待遇结束日期不能为空', trigger: 'blur' }
        ],
        bisZhsqXsdyq: [
          { required: true, message: '享受待遇期不能为空', trigger: 'blur' }
        ],
        bisZhsqAdl: [
          { required: true, message: 'ADL量表评分不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    /**
     * 点击修改之前校验
     * @param crud
     * @param form
     * @returns {boolean}
     */
    [CRUD.HOOK.beforeToEdit](crud, form) {
      // 清空附件列表
      this.files = []
      this.files2 = []
      this.tempFiles = []
      this.getFiles()
      this.roleId = this.user.roles[0].id
      return true
    },
    openOrgList(row) {
      this.$refs.completeOrgList.detailDialog = true
      this.$refs.completeOrgList.ddbm = row.bisHljgYljgbh
      this.$refs.completeOrgList.crud.toQuery()
    },
    /**
     * 修改操作时，需要加载附件信息
     */
    getFiles: function() {
      crudFile.queryFileByFjlb2(this.form.bisZhsqId).then(res => {
        this.tempFiles = res
        for (let i = 0; i < res.length; i++) {
          this.files.push(res[i].path3)
          this.files2.push(res[i].path2)
        }
      })
    },
    /**
     * 打开ADL量表
     */
    openAdl() {
      console.log(this.user)
      this.adlDrawer = true
      this.getPgAdl()
    },
    /**
     * 打开ADL量表时，实时查询
     */
    getAdl() {
      console.log(this.form.bisZhsqAdlPc)
      getAdlXmxx({ adlPcId: this.form.bisZhsqAdlPc }).then(res => {
        console.log(res)
        this.adlData = res.adlData
        this.form.bisZhsqAdlPc = res.adlPcId
      })
    },
    /**
     * 打开ADL量表时，实时查询
     */
    getPgAdl() {
      queryPgAdlResult(this.form.bisZhsqId, this.form.bisZhsqAdlPc).then(res => {
        console.log(res)
        this.adlData = res.adlData
      })
    },
    /**
     * ADL量表关闭
     * @param done
     */
    handleClose(done) {
      this.adlDrawer = false
    },
    /**
     * 自定义保存
     * @param zt
     */
    doSave(zt) {
      this.form.bisZhsqZt = zt
      const titleMsg = zt === 'zhzt03' ? '复核通过' : '复核不通过'
      if (this.crud.status.add === 1 && this.crud.status.edit === 0) { // 点击新增按钮，提交的时候只是新增
        this.crud.status.add = CRUD.STATUS.PROCESSING
        crudZhsq.add(this.form)
          .then(res => {
            this.crud.status.add = CRUD.STATUS.NORMAL
            this.$notify({
              title: titleMsg,
              type: 'success',
              duration: 2500
            })
            this.crud.toQuery()
          })
          .catch(err => {
            this.crud.status.add = CRUD.STATUS.PREPARED
            console.log(err)
          })
      } else if (this.crud.status.add === 0 && this.crud.status.edit === 1) { // 点击修改按钮，提交的时候只是修改
        this.crud.status.edit = CRUD.STATUS.PROCESSING
        crudZhsq.edit(this.form)
          .then(res => {
            this.crud.status.edit = CRUD.STATUS.NORMAL
            this.$notify({
              title: titleMsg,
              type: 'success',
              duration: 2500
            })
            this.crud.toQuery()
          })
          .catch(err => {
            this.crud.status.edit = CRUD.STATUS.PREPARED
            console.log(err)
          })
      }
    }
  }
}
</script>

<style scoped>

</style>
